import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import '../../flutter_base_plugin.dart';
export '../../flutter_base_plugin.dart';
abstract class BaseFunction {
  late State _baseState;
  late BuildContext _baseBuildContext;
  bool isShowLoadWidget = true;

  bool _isTopBarShow = false; //状态栏是否显示

  bool _isAppBarShow = false; //标题栏是否显示

  late Color _appBarColor;

  late Color _topBarColor;

  late Color _appBarTitleColor;

  //标题字体大小
  late double _appBarTitleSize; //根据需求变更

  String _appBarTitle = "";

  late Color _backgroundColor;

  Color get backgroundColor => _backgroundColor;

  //小标题信息
  String _appBarRightTitle = "";

  double _appBarRightTitleSize = 15.0;

  late Color _appBarRightTitleColor;

  Color get appBarRightTitleColor => _appBarRightTitleColor;

  bool _isBackIconShow = true;

  double bottomVertical = 0; //作为内部页面距离底部的高度

  Function()? _rightOnClick;

  void initBaseCommon(State state, BuildContext context) async {
    _baseState = state;
    _baseBuildContext = context;
    if (BasePlugin.appConfig.configAppBarColor() != null) {
      _appBarColor = BasePlugin.appConfig.configAppBarColor();
      _topBarColor = BasePlugin.appConfig.configAppBarColor();
    } else {
      _appBarColor = BColor.appBarColor;
      _topBarColor = BColor.appBarColor;
    }
    if (BasePlugin.appConfig.configAppBarTitleColor() != null) {
      _appBarTitleColor = BasePlugin.appConfig.configAppBarTitleColor();
    } else {
      _appBarTitleColor = Colors.white;
    }
    if (BasePlugin.appConfig.configWidgetBackgroundColor() != null) {
      _backgroundColor = BasePlugin.appConfig.configWidgetBackgroundColor();
    } else {
      _backgroundColor = const Color(0xFFF4F4F4);
    }
    if (BasePlugin.appConfig.configAppBarTitleSize() < 10) {
      _appBarTitleSize = 20;
    } else {
      _appBarTitleSize = BasePlugin.appConfig.configAppBarTitleSize();
    }
  }

  Widget getBaseTopBar() {
    return Offstage(
      offstage: !_isTopBarShow,
      child: _getTopBar(),
    );
  }

  Widget getBaseAppBar() {
    return Offstage(
      offstage: !_isAppBarShow,
      child: buildAppBar(),
    );
  }

  ///设置状态栏，可以自行重写拓展成其他的任何形式
  Widget _getTopBar() {
    return Container(
      height: getTopBarHeight(),
      width: double.infinity,
      color: _topBarColor,
    );
  }

  ///导航栏appBar中间部分 ，不满足可以自行重写
  Widget buildAppBarTitle() {
    return Text(
      _appBarTitle,
      style: TextStyle(
        fontSize: _appBarTitleSize,
        color: _appBarTitleColor,
        fontWeight: FontWeight.w600,
      ),
    );
  }

  Widget buildAppBarRight() {
    return InkWell(
      onTap: _rightOnClick,
      child: Text(
        _appBarRightTitle,
        style: TextStyle(
          fontSize: _appBarRightTitleSize,
          color: _appBarTitleColor,
        ),
      ),
    );
  }

  ///导航栏appBar左边部分 ，不满足可以自行重写
  Widget buildAppBarLeft() {
    return IconButton(
      onPressed: () {
        clickAppBarBack();
      },
      icon: Icon(
        Icons.arrow_back_ios,
        color: _appBarTitleColor,
      ),
    );
  }

  void clickAppBarBack() {
    if (Navigator.canPop(_baseBuildContext)) {
      Navigator.of(_baseBuildContext).pop("refresh");
    } else {
      //说明已经没法回退了 ， 可以关闭了
      finishDartPageOrApp();
    }
  }

  ///返回中间可绘制区域，也就是 我们子类 buildWidget 可利用的空间高度
  double getMainWidgetHeight() {
    double screenHeight = getScreenHeight() - bottomVertical;
    if (_isTopBarShow) {
      screenHeight = screenHeight - getTopBarHeight();
    }
    if (_isAppBarShow) {
      screenHeight = screenHeight - getAppBarHeight();
    }
    return screenHeight;
  }

  ///返回屏幕高度
  double getScreenHeight() {
    // return MediaQuery.of(_baseBuildContext).size.height;
    return ScreenUtil().screenHeight;
  }

  ///返回状态栏高度
  double getTopBarHeight() {
    return MediaQuery.of(_baseBuildContext).padding.top;
  }

  ///返回appbar高度，也就是导航栏高度
  double getAppBarHeight() {
    return kToolbarHeight;
  }

  ///返回屏幕宽度
  double getScreenWidth() {
    return MediaQuery.of(_baseBuildContext).size.width;
  }

  ///关闭最后一个 flutter 页面 ， 如果是原生跳过来的则回到原生，否则关闭app
  void finishDartPageOrApp() {
    SystemNavigator.pop();
  }

  ///导航栏 appBar 可以重写
  Widget buildAppBar() {
    return Container(
      height: getAppBarHeight(),
      width: double.infinity,
      color: _appBarColor,
      child: Stack(
        alignment: const FractionalOffset(0, 0.5),
        children: <Widget>[
          Align(
            alignment: const FractionalOffset(0.5, 0.5),
            child: buildAppBarTitle(),
          ),
          // getAppBarLeft(),
          Offstage(
            offstage: !_isBackIconShow,
            child: Align(
              alignment: const FractionalOffset(0.03, 0.5),
              child: buildAppBarLeft(),
            ),
          ),
          Align(
            alignment: const FractionalOffset(0.96, 0.5),
            child: buildAppBarRight(),
          ),
        ],
      ),
    );
  }

  ///设置状态栏隐藏或者显示
  void setTopBarVisible(bool isVisible) {
    // ignore: invalid_use_of_protected_member
    _baseState.setState(() {
      _isTopBarShow = isVisible;
    });
  }

  ///默认这个状态栏下，设置颜色
  void setTopBarBackColor(Color color) {
    // ignore: invalid_use_of_protected_member
    _baseState.setState(() {
      if (color == null) {
        _topBarColor = _topBarColor;
      } else {
        _topBarColor = color;
      }
    });
  }

  ///设置Widget背景颜色
  void setBackgroundColor(Color color) {
    // ignore: invalid_use_of_protected_member
    _baseState.setState(() {
      if (color == null) {
        _backgroundColor = _backgroundColor;
      } else {
        _backgroundColor = color;
      }
    });
  }

  ///设置导航栏的字体以及图标颜色
  void setAppBarTitleColor(Color contentColor) {
    _baseState.setState(() {
      _appBarTitleColor = contentColor;
    });
  }

  ///设置导航栏的字体以及图标颜色
  void setAppBarRightTitleColor(Color contentColor) {
    _baseState.setState(() {
      _appBarRightTitleColor = contentColor;
    });
  }

  ///设置导航栏隐藏或者显示
  void setAppBarVisible(bool isVisible) {
    // ignore: invalid_use_of_protected_member
    _baseState.setState(() {
      _isAppBarShow = isVisible;
      _isTopBarShow = isVisible;
    });
  }

  ///默认这个导航栏下，设置颜色
  void setAppBarColor(Color color) {
    // ignore: invalid_use_of_protected_member
    _baseState.setState(() {
      _appBarColor = color;
    });
  }

  void setAppBarTitle(String title) {
    _baseState.setState(() {
      _appBarTitle = title;
      _isTopBarShow = true; //状态栏是否显示
      _isAppBarShow = true; //标题栏是否显示
    });
  }

  void setAppBarRightTitle(String title, Function() function) {
    _baseState.setState(() {
      _appBarRightTitle = title;
      _rightOnClick = function;
    });
  }

  void hideBackIcon() {
    _baseState.setState(() {
      _isBackIconShow = false;
    });
  }

  ///初始化一些变量 相当于 onCreate ， 放一下 初始化数据操作
  void buildCreate();

  void log(String content) {
    if (BuildConfig.isDebug) {
      print("${getClassName()}------>$content");
    }
  }

  String getClassName() {
    if (_baseBuildContext == null) {
      return "";
    }
    String className = _baseBuildContext.toString();
    if (className == null) {
      return "";
    }

    if (BuildConfig.isDebug) {
      try {
        className = className.substring(0, className.indexOf("("));
      } catch (err) {
        className = "";
      }
      return className;
    }

    return className;
  }
}
